import { isDef } from '@/common/utils'
export default {
	//后续优化封装部分方法在utils的公共方法内
	//common
	userInfoTrans(state){
	   const {user} = state
	   let userInfoTransList={
		   'dm':'管理员',
		   'player':'玩家',
		   'guest':'游客'
	   }
	   return userInfoTransList[user.type]
	},
	//dm
	//当前游戏设置的游戏人数
	gamePlayerNum(state) {
		const {gameStatus,currentGameRoleListConfirm,currentGameDTO} = state
		if(Object.keys(currentGameDTO)>0 ){ return currentGameDTO.setting.playerCount}
		return  currentGameRoleListConfirm.length
	},
	//当前游戏设置的角色清单
	gameRoleInfo(state) {
		const {currentGameRoleListConfirm} = state
		return new Set(currentGameRoleListConfirm)
	},
	//当前游戏对应角色
	gameRoleTrans(state) {
		const {currentChoiceRole} = state
		let gameRoleTransList = {
			'白痴':'bc',
			'丘比特':'cupid',
			'守卫':'guarder',
			'猎人':'hunter',
			'预言家':'seer',
			'平民':'villager',
			'普通狼人':'werewolf',
			'白狼王':'white_wolf_king',
			'女巫':'witch',
			'狼美人':'wolf_beauty',
			'狼王':'wolf_king'
		}

		return gameRoleTransList[currentChoiceRole]

	},
	//当前游戏对应进度
	gameProcessStatus(state) {
		const {gameStatus} = state
		let gameStatusTransList = {
			'setting': '待创建',
			'init': '初始化',
			'human_win': '人类胜利',
			'process': '游戏进行中',
			'third_part_win': '第三方阵营胜利',
			' wolf_win': '狼人胜利'
		}

		return gameStatusTransList[gameStatus]
	},
	//当前游戏对应回合
	gameStageCodeTrans(state) {
		const {gameStageCode} = state
		let gameStageCodeTransList = {
			'first_night': "首夜",
			'police_election': "警长竞选",
			'day_1': "第一日",
			'night_1': "第一夜",
			'day_2': "第二日",
			'night_2': "第二夜",
			'day_3': "第三日",
			'night_3': "第三夜",
			'day_4': "第四日",
			'night_4': "第四夜",
			'day_5': "第五日",
			'night_5': "第五夜",
			'day_6': "第六日",
			'night_6': "第六夜",
			'day_7': "第七日",
			'night_7': "第七夜",
			'day_8': "第八日",
			'night_8': "第八夜",
			'day_9': "第九日",
			'night_9': "第九夜",
			'day_10': "第十日",
			'night_10': "第十夜",
		}
		return gameStageCodeTransList[gameStageCode]

	},
	//当前游戏的投票记录
	gameVoteResultList(state) {
		const {stageInfoMap,currentGameDTO} = state
		//总投票列表
		let voteRecordList = []
		//每个回合的投票信息
		let gameStagevoteInfo = {}
		for (let i = 0; i < stageInfoMap.length; i++) {	
			//回合内投票对应的阶段
			gameStagevoteInfo.gameStage =Object.keys(stageInfoMap[i])[0]
			console.log(gameStagevoteInfo)
			//回合内投票对应的总投票记录
			let voteTurn = gameStagevoteInfo.voteRecord = {}
			//回合内投票单轮投票记录
			let turnRecord=[]
			for (let h = 0; h < Object.keys(stageInfoMap[i].voteInfoMap).length; h++) {
				let allowVotedPlayer = stageInfoMap[i].voteInfoMap[h].limit.allowVotedPlayer
				if(allowVotedPlayer.length=0){allowVotedPlayer=Array.from(new Array(currentGameDTO.setting.playerCount + 1).keys()).slice(1)}
				console.log(allowVotedPlayer)
					//循环遍历出允许被投票的玩家 进行查找匹配票数
					let voteCount = []
					for (let j = 0; j < allowVotedPlayer.length; j++) {
						voteCount=stageInfoMap[i].voteInfoMap[h].records.filter(n => {return n.votedPlayer === allowVotedPlayer[j]})
						let tmpVotePlayer = []
						for (let k = 0; k < voteCount.length; k++) {
							tmpVotePlayer.push(voteCount[k].votePlayer)
						}
						turnRecord.push({
							seatNum: allowVotedPlayer[j],
							votedCount: voteCount.length,
							voteplayers: tmpVotePlayer +"",
						})
						//清空数组方式待优化
						voteCount.splice(0)
						tmpVotePlayer.splice(0)	
					}
					voteTurn = voteTurn.concat(turnRecord)
					turnRecord.splice(0)
			}
			voteRecordList.push(gameStagevoteInfo)
			gameStagevoteInfo.splice(0)
		}
		return voteRecordList
	},
	//当前游戏回合所处的投票轮次
	gameStageCodeVoteTurns(state){
		const {stageInfoMap,gameStageCode} = state
		if(Object.keys(stageInfoMap)>0 ){
			return Object.keys(stageInfoMap[gameStageCode].voteInfoMap).length
		} 
		return 1
	},
	
	//player
	gameSeatList(state){
		const {user,currentGameDTO} = state
		let filterID=user.id
		let clsIndex=[]
		if(Object.keys(currentGameDTO).length >0)
		{clsIndex= currentGameDTO.players.filter(n => {return n.user.id === filterID})[0] || {seatNumber:0}}
		console.log(clsIndex)
		let seatList = []
		for (let i = 1; i <= currentGameDTO.setting.playerCount; i++) {
			let seatInfo={}
			let seatTransAndCls={
				'bc':{trans:'白痴',type:"success"},
				'cupid':{trans:'丘比特',type:"success"} ,
				'guarder':{trans:'守卫',type:"success"} ,
				'hunter':{trans:'猎人',type:"success"},
				'seer':{trans:'预言家',type:"success"},
				'villager':{trans:'平民',type:"primary"},
				'werewolf':{trans:'狼人',type:"error"},
				'white_wolf_king':{trans:'白狼王',type:"error"},
				'witch':{trans:'女巫',type:"success"},
				'wolf_beauty':{trans:'狼美人',type:"warning"},
				'wolf_king':  {trans:'狼王',type:"error"},
				'null': {trans:'未录入',type:"primary"}
				}

			seatInfo.seatNum=i
			i==clsIndex.seatNumber?seatInfo.seatStatusCls="selected-mine":seatInfo.seatStatusCls="selected-order"
			seatInfo.seatAvatar="/static/uni-center/grey.png"
			user.type ==="dm"?seatInfo.seatAvatarRole="未录入":seatInfo.seatAvatarRole="**身份"
			seatInfo.seatRoleType="primary"
			seatInfo.seatRoleStatus=""
			let checkSeatStatus = currentGameDTO.players.filter(n => {
				return n.seatNumber=== i
			})
			if(checkSeatStatus.length > 0 ){
				seatInfo.seatAvatar=checkSeatStatus[0].user.avatar
				seatInfo.seatRoleStatus=checkSeatStatus[0].status
				if(user.type ==="dm"){
					seatInfo.seatAvatarRole=seatTransAndCls[checkSeatStatus[0].gameRole.code].trans || "null"
					seatInfo.seatRoleType=seatTransAndCls[checkSeatStatus[0].gameRole.code].type
				}
			}
			checkSeatStatus.splice(0)	
			seatList.push(seatInfo)
		}
		return seatList
	}
}
